﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine(myorder1.getPriceInAll());
            orderitem laowangsorderitem1 = new orderitem("apple", 2, 6.6);
            orderitem laowangsorderitem2 = new orderitem("pear", 3, 7.7);
            List<orderitem> orderitems1 = new List<orderitem>();
            orderitems1.Add(laowangsorderitem1);
            Order myorder1 = new Order(1, "laowang", "2003-01-25", orderitems1);
            myorder1.showorderItem();
            OrderService myorderService = new OrderService();
            orderitems1.Add(laowangsorderitem2);
            myorderService.addOrder(myorder1);
            myorderService.refreshOrder(orderitems1);
            myorderService.searchOrder(2);
            myorderService.removeOrder();
            
        }
    }

    class orderitem{//订单中一个item的详细信息

        private string name;
        public string Name{
            get{
                return name;
            }
            set{
                name = value;
            }
        }

        private int number;
        public int Number{
            get{
                return number;
            }
            set{
                if(number>=0) number = value;
                else Console.WriteLine("item的数量必须大于0");
            }
        }

        private double price;
        public double Price{
            get{
                return price;
            }
            set{
                if(price>=0) price = value;
                else Console.WriteLine("item的价格必须大于0");
            }
        }

        public orderitem(){//无参构造函数
            this.Name = string.Empty;
            this.Number = 0;
            this.Price = 0;
        }

        public double getPrice(){
            return this.Number * this.Price;
        }

        public orderitem(string name, int number,double price){
            this.Name = name;
            this.number = number;
            this.price = price;
        }


    }

    class Order//单个订单
    {
        
        public int id{get;set;}
        public string customer{get;set;}
        public double Money{get;set;}
        public string Date{get;set;}

        public List<orderitem> orderitems = new List<orderitem>();

        public Order(){//无参构造函数
            this.id = 0;
            this.customer = string.Empty;
            this.Money = 0;
            this.Date = string.Empty;
        }

        public Order(int id,string customer,string Date,List<orderitem> thisorderitems){//有参构造函数
            this.id = id;
            this.customer = customer;
            this.Date = Date;
            this.orderitems = thisorderitems;
        }

        public double getPriceInAll(){
            double result = 0;
            foreach(orderitem a in this.orderitems){
                result = result + a.getPrice();//getPrice返回的是一个订单中每一个item它的钱 这个钱实际上是该item的数量乘以该item的单价
            }
            return result;
        }
        // public void addOrderItem(string name,int number,double price){
        //     orderitem a = new orderitem(name,number,price);
        //     this.orderitems.Add(a);
        // }
        public void showorderItem(){
            Console.WriteLine("序号 名称 数量 单价");
            foreach(orderitem a in this.orderitems){
                Console.WriteLine("--------------------");
                Console.WriteLine("{0} {1} {2} {3}",this.orderitems.IndexOf(a),a.Name,a.Number,a.Price);
            }
        }
    }

    class OrderService
    {
        public List<Order> order = new List<Order>();

        public void addOrder(Order newOrder)
        {
            this.order.Add(newOrder);
        }

        public void removeOrder()
        {
            try
            {
                Console.WriteLine("请输入要删除的订单id：");
                int deleteId = int.Parse(Console.ReadLine());
                int Index = 0;
                foreach(Order a in this.order)
                {
                    if (a.id == deleteId) Index = this.order.IndexOf(a);
                }
                this.order.RemoveAt(Index);
            }
            catch
            {
                Console.WriteLine("输入错误");
            }
        }

        public void refreshOrder(List<orderitem> orderitemlist){
            try{
                Console.WriteLine("请输入你要修改的订单id：");
                int deleteId = int.Parse(Console.ReadLine());
                int Index = 0;
                foreach(Order a in this.order){
                    if(a.id == deleteId) Index = this.order.IndexOf(a);
                }
                Console.WriteLine("该订单的customer是：");
                this.order[Index].customer = Console.ReadLine();
                Console.WriteLine("该订单的Date是：");
                this.order[Index].Date = Console.ReadLine();
                Console.WriteLine("该订单的orderitemlist是：");
                this.order[Index].orderitems = orderitemlist;
                //Console.WriteLine("该订单的Money是：");
                this.order[Index].Money = this.order[Index].getPriceInAll();
                this.order[Index].showorderItem();
            }
            catch{
                Console.WriteLine("输入错误");
            }
        }

        public void searchOrder(int i)
        {
            try
            {
                switch(i)
                {
                    case 1: //按金额进行查询
                    int maxnum;int minnum;
                    Console.WriteLine("请输入要查询的最小金额");   
                    minnum = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请输入要查询的最大金额");
                    maxnum = Convert.ToInt32(Console.ReadLine());

                    var query1 = from s1 in order
                                 where maxnum > s1.Money
                                 orderby s1.Money
                                 select s1;
                    var query2 = from s2 in query1
                                 where minnum < s2.Money
                                 orderby s2.Money
                                 select s2;
                    List<Order>  a1 = query2.ToList();  
                    Console.WriteLine("订单号   顾客    日期    订单价格");
                    foreach(Order b1 in a1){
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("{0} {1} {2} {3}",b1.id,b1.customer,b1.Date,b1.Money);
                    }
                    break;

                    case 2://按客户名进行查询
                        Console.WriteLine("请输入客户名称：");
                        string name = Console.ReadLine();
                        var query3 = from s3 in this.order
                                     where s3.customer == name
                                     orderby s3.Money
                                     select s3;
                        List<Order> a2 = query3.ToList();
                        Console.WriteLine("订单号   顾客    日期    订单价格");
                        foreach(Order b2 in a2){
                            Console.WriteLine("-----------------------------------");
                            Console.WriteLine("{0} {1} {2} {3}",b2.id,b2.customer,b2.Date,b2.Money);
                        }
                        break;
                    case 3:
                        Console.WriteLine("请输入订单编号");
                        int id = int.Parse(Console.ReadLine());
                        var query4 = from s4 in this.order
                                     where s4.id == id
                                     orderby s4.Money
                                     select s4;
                        List<Order> a3 = query4.ToList();
                        Console.WriteLine("订单号   顾客   日期   订单价格");
                        foreach(Order b3 in a3){
                            Console.WriteLine("-------------------------------------------");
                            Console.WriteLine("{0} {1} {2} {3}",b3.id,b3.customer,b3.Date,b3.Money);
                        }
                        break;

                    case 4:
                        Console.WriteLine("请输入商品名称：");
                        string itemName = Console.ReadLine();
                        var query5 = from s5 in this.order
                                     where s5.orderitems.Exists(
                                        x => x.Name == itemName
                                     )
                                     orderby s5.Money
                                     select s5;
                        List<Order> a4 = query5.ToList();
                        Console.WriteLine("订单号   顾客   日期   订单价格");
                        foreach(Order b4 in a4){
                            Console.WriteLine("--------------------------------------------");
                            Console.WriteLine("{0} {1} {2} {3}",b4.id,b4.customer,b4.Date,b4.Money);
                        }
                        break;
                    default:
                        Console.WriteLine("您输入的选项不存在");
                        break;
                }
            }
            catch{
                Console.WriteLine("输入错误");
            }

        }

    }

}
